/************************************************************************************/
/* Manuscript Title: Accelerated Late Gadolinium Enhancement CMR with Generative AI */
/************************************************************************************/



/**********************/
/**********************/
/* I. IMAGE SHARPNESS */
/**********************/
/**********************/


/***************/
/* IMPORT DATA */
/***************/

PROC IMPORT OUT=blur
            DATAFILE= "INSERT_FILEPATH.xlsx"
            DBMS=xlsx REPLACE;
			SHEET="New_Data";
			GETNAMES=YES;
RUN;
*n=100, 31 variables;

PROC SORT DATA=blur; BY subject_ID; RUN;



/*****************************************/
/* TRANSPOSE DATA FROM WIDE TO LONG FORM */
/*****************************************/
*Data are currently in wide format (1 row per subject_id), transpose to long format where there is 1 row per observation (3 rows per subject_id);

/* Macro for data transposition */
%MACRO tran1(num_vars, ds, image);

DATA blur1 (KEEP=subject_id &num_vars.);
     SET blur;
RUN;

PROC TRANSPOSE DATA=blur1 OUT=&ds.;
     BY subject_id;
RUN;

DATA &ds.;
     ATTRIB subject_id reader LENGTH=8.;
     SET &ds. (RENAME=(col1=&image.));
     reader=input(SUBSTR(_NAME_, 2,1)*1, 12.);
     DROP _name_ _label_;
RUN;

PROC SORT DATA=&ds.; BY subject_id reader; RUN;
%MEND;


*Image 1;
%LET num_vars = r1_b_i1 r2_b_i1 r3_b_i1;
%tran1(&num_vars., b_image1, image1);

*Image 2;
%LET num_vars = r1_b_i2 r2_b_i2 r3_b_i2;
%tran1(&num_vars., b_image2, image2);

*Image 3;
%LET num_vars = r1_b_i3 r2_b_i3 r3_b_i3;
%tran1(&num_vars., b_image3, image3);

*Image 4;
%LET num_vars = r1_b_i4 r2_b_i4 r3_b_i4;
%tran1(&num_vars., b_image4, image4);

*Image 5;
%LET num_vars = r1_b_i5 r2_b_i5 r3_b_i5;
%tran1(&num_vars., b_image5, image5);


/* Merge datasets */
DATA blur_final;
     MERGE b_image1 b_image2 b_image3 b_image4 b_image5;
     BY subject_id reader;
RUN;
*n=300, 7 variables;



/*************************************/
/* CREATE NEW VARIABLES FOR ANALYSIS */
/*************************************/

DATA blur_final1;
     SET blur_final;
	 diff_2and1=(image2-image1);
	 diff_3and1=(image3-image1);
	 diff_4and1=(image4-image1);
	 diff_5and1=(image5-image1);
	 diff_3and2=(image3-image2);
	 diff_5and4=(image5-image4);

	 IF diff_2and1 LT 0 THEN improve_2and1=1;
	 IF diff_2and1 GT -1 THEN improve_2and1=0;
	 ELSE IF diff_2and1='.' THEN improve_2and1='.';

	 IF diff_3and1 LT 0 THEN improve_3and1=1;
	 IF diff_3and1 GT -1 THEN improve_3and1=0;
	 ELSE IF diff_3and1='.' THEN improve_3and1='.';

	 IF diff_4and1 LT 0 THEN improve_4and1=1;
	 IF diff_4and1 GT -1 THEN improve_4and1=0;
	 ELSE IF diff_4and1='.' THEN improve_4and1='.';

	 IF diff_5and1 LT 0 THEN improve_5and1=1;
	 IF diff_5and1 GT -1 THEN improve_5and1=0;
	 ELSE IF diff_5and1='.' THEN improve_5and1='.';

	 IF diff_3and2 LT 0 THEN improve_3and2=1;
	 IF diff_3and2 GT -1 THEN improve_3and2=0;
	 ELSE IF diff_3and2='.' THEN improve_3and2='.';

	 IF diff_5and4 LT 0 THEN improve_5and4=1;
	 IF diff_5and4 GT -1 THEN improve_5and4=0;
	 ELSE IF diff_5and4='.' THEN improve_5and4='.';
RUN;
*n=300, 19 variables;



/**********************/
/* LINEAR MIXED MODEL */
/**********************/

/* Macro for frequency of improvement and linear mixed model */
%MACRO mixed(ds, numeric_var);

*Overall frequency;
PROC FREQ DATA= &ds.;
     TABLES &numeric_var.;
RUN; 

*Frequency by reader;
PROC FREQ DATA= &ds.;
     TABLES reader*&numeric_var. / NOPERCENT NOCOL;
RUN; 

*Linear mixed model;
PROC MIXED DATA= &ds.;
           CLASS subject_id reader (REF='1');
           MODEL &numeric_var. = reader / S CL;      *S: solution for the fixed-effects parameters be produced, CL:displays confidence limits for fixed-effects parameter estimates;
           REPEATED / SUBJECT=subject_id TYPE=cs R;  *R: blocks of the estimated matrix, CS: compound symmetry covariance structure;
           ESTIMATE 'Average Improvement from R1 R2 R3' int 1 / CL E;

		   ESTIMATE 'Improvement R1' int 1 reader 0 0 1 / CL E; 
           ESTIMATE 'Improvement R2' int 1 reader 1 0 0 / CL E; 
           ESTIMATE 'Improvement R3' int 1 reader 0 1 0 / CL E; 
RUN;
%MEND;


/* Macro for percent improvement in image sharpness between scans by reader  */
%LET numeric_var = improve_2and1;    /*IMAGES 2 & 1*/
%mixed(blur_final1, &numeric_var.);

%LET numeric_var = improve_3and1;    /*IMAGES 3 & 1*/
%mixed(blur_final1, &numeric_var.);

%LET numeric_var = improve_4and1;    /*IMAGES 4 & 1*/
%mixed(blur_final1, &numeric_var.);

%LET numeric_var = improve_5and1;    /*IMAGES 5 & 1*/
%mixed(blur_final1, &numeric_var.);

%LET numeric_var = improve_3and2;    /*IMAGES 3 & 2*/
%mixed(blur_final1, &numeric_var.);

%LET numeric_var = improve_5and4;    /*IMAGES 5 & 4*/
%mixed(blur_final1, &numeric_var.);





/**************************/
/**************************/
/* II. DIAGNOSTIC QUALITY */
/**************************/
/**************************/



/***************/
/* IMPORT DATA */
/***************/

PROC IMPORT OUT=dq
            DATAFILE= "INSERT_FILEPATH.xlsx"
            DBMS=xlsx REPLACE;
			SHEET="New_Data";
			GETNAMES=YES;
RUN;
*n=100, 31 variables;

PROC SORT DATA=dq; BY subject_ID; RUN;



/*****************************************/
/* TRANSPOSE DATA FROM WIDE TO LONG FORM */
/*****************************************/
*Data are currently in wide format (1 row per subject_id), transpose to long format where there is 1 row per observation (3 rows per subject_id);

/* Macro for data transposition */
%MACRO tran2(num_vars, ds, image);

DATA dq1 (KEEP=subject_id &num_vars.);
     SET dq;
RUN;

PROC TRANSPOSE DATA=dq1 OUT=&ds.;
     BY subject_id;
RUN;

DATA &ds.;
     ATTRIB subject_id reader LENGTH=8.;
     SET &ds. (RENAME=(col1=&image.));
     reader=input(SUBSTR(_NAME_, 2,1)*1, 12.);
     DROP _name_ _label_;
RUN;

PROC SORT DATA=&ds.; BY subject_id reader; RUN;
%MEND;


*Image 1;
%LET num_vars = r1_d_i1 r2_d_i1 r3_d_i1;
%tran2(&num_vars., dq_image1, image1);

*Image 2;
%LET num_vars = r1_d_i2 r2_d_i2 r3_d_i2;
%tran2(&num_vars., dq_image2, image2);

*Image 3;
%LET num_vars = r1_d_i3 r2_d_i3 r3_d_i3;
%tran2(&num_vars., dq_image3, image3);

*Image 4;
%LET num_vars = r1_d_i4 r2_d_i4 r3_d_i4;
%tran2(&num_vars., dq_image4, image4);

*Image 5;
%LET num_vars = r1_d_i5 r2_d_i5 r3_d_i5;
%tran2(&num_vars., dq_image5, image5);


/* Merge datasets */
DATA dq_final;
     MERGE dq_image1 dq_image2 dq_image3 dq_image4 dq_image5;
     BY subject_id reader;
RUN;
*n=300, 7 variables;



/*************************************/
/* CREATE NEW VARIABLES FOR ANALYSIS */
/*************************************/

DATA dq_final1;
     SET dq_final;
	 diff_2and1=(image2-image1);
	 diff_3and1=(image3-image1);
	 diff_4and1=(image4-image1);
	 diff_5and1=(image5-image1);
	 diff_3and2=(image3-image2);
	 diff_5and4=(image5-image4);

	 IF diff_2and1 LT 0 THEN improve_2and1=1;
	 IF diff_2and1 GT -1 THEN improve_2and1=0;
	 ELSE IF diff_2and1='.' THEN improve_2and1='.';

	 IF diff_3and1 LT 0 THEN improve_3and1=1;
	 IF diff_3and1 GT -1 THEN improve_3and1=0;
	 ELSE IF diff_3and1='.' THEN improve_3and1='.';

	 IF diff_4and1 LT 0 THEN improve_4and1=1;
	 IF diff_4and1 GT -1 THEN improve_4and1=0;
	 ELSE IF diff_4and1='.' THEN improve_4and1='.';

	 IF diff_5and1 LT 0 THEN improve_5and1=1;
	 IF diff_5and1 GT -1 THEN improve_5and1=0;
	 ELSE IF diff_5and1='.' THEN improve_5and1='.';

	 IF diff_3and2 LT 0 THEN improve_3and2=1;
	 IF diff_3and2 GT -1 THEN improve_3and2=0;
	 ELSE IF diff_3and2='.' THEN improve_3and2='.';

	 IF diff_5and4 LT 0 THEN improve_5and4=1;
	 IF diff_5and4 GT -1 THEN improve_5and4=0;
	 ELSE IF diff_5and4='.' THEN improve_5and4='.';
RUN;
*n=300, 19 variables;



/**********************/
/* LINEAR MIXED MODEL */
/**********************/

/* Macro for frequency of improvement and linear mixed model */
%MACRO mixed(ds, numeric_var);

*Overall frequency;
PROC FREQ DATA= &ds.;
     TABLES &numeric_var.;
RUN; 

*Frequency by reader;
PROC FREQ DATA= &ds.;
     TABLES reader*&numeric_var. / NOPERCENT NOCOL;
RUN; 

*Linear mixed model;
PROC MIXED DATA= &ds.;
           CLASS subject_id reader (ref='1');
           MODEL &numeric_var. = reader / S CL;      *S: solution for the fixed-effects parameters be produced, CL:displays confidence limits for fixed-effects parameter estimates;
           REPEATED / SUBJECT=subject_id TYPE=cs R;  *R: blocks of the estimated matrix, CS: compound symmetry covariance structure;
           ESTIMATE 'Average Improvement from R1 R2 R3' int 1 / CL E;

		   ESTIMATE 'Improvement R1' int 1 reader 0 0 1 / CL E; 
           ESTIMATE 'Improvement R2' int 1 reader 1 0 0 / CL E; 
           ESTIMATE 'Improvement R3' int 1 reader 0 1 0 / CL E; 
RUN;
%MEND;


/* Macro for percent improvement in image diagnostic quality between scans by reader  */
%LET numeric_var = improve_2and1;    /*IMAGES 2 & 1*/
%mixed(dq_final1, &numeric_var.);

%LET numeric_var = improve_3and1;    /*IMAGES 3 & 1*/
%mixed(dq_final1, &numeric_var.);

%LET numeric_var = improve_4and1;    /*IMAGES 4 & 1*/
%mixed(dq_final1, &numeric_var.);

%LET numeric_var = improve_5and1;    /*IMAGES 5 & 1*/
%mixed(dq_final1, &numeric_var.);

%LET numeric_var = improve_3and2;    /*IMAGES 3 & 2*/
%mixed(dq_final1, &numeric_var.);

%LET numeric_var = improve_5and4;    /*IMAGES 5 & 4*/
%mixed(dq_final1, &numeric_var.);

















